<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="员工姓名" prop="staffName">
        <el-input
          v-model="queryParams.staffName"
          placeholder="请输入员工姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工编号" prop="staffNo">
        <el-input
          v-model="queryParams.staffNo"
          placeholder="请输入员工编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工性别" prop="gender">
        <el-select v-model="queryParams.gender" placeholder="请选择员工性别" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属车间" prop="workshopId">
        <el-select v-model="queryParams.workshopId" placeholder="请选择车间" clearable>
          <el-option
            v-for="item in workshopList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属班组" prop="groupId">
        <el-cascader
          v-model="queryParams.groupId"
          :options="this.workshopList"
          :props="{ expandTrigger: 'hover', emitPath:false }" clearable></el-cascader>
      </el-form-item>
      <el-form-item label="岗位" prop="postNo">
        <el-select v-model="queryParams.postNo" placeholder="请选择岗位" clearable>
          <el-option
            v-for="item in sysPostList"
            :key="item.postCode"
            :label="item.postName"
            :value="item.postCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否在职" prop="working">
        <el-select v-model="queryParams.working" placeholder="请选择是否在职" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['aero:staff:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['aero:staff:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['aero:staff:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['aero:staff:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="staffList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="员工姓名" align="center" prop="staffName" />
      <el-table-column label="员工编号" align="center" prop="staffNo" />
      <el-table-column label="员工性别" align="center" prop="gender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.gender"/>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="所属车间编码" align="center" prop="workshopInfo.workshopNo" />
      <el-table-column label="所属班组编码" align="center" prop="groupInfo.groupNo" />
      <el-table-column label="岗位" align="center" prop="sysPost.postName" />
      <el-table-column label="是否在职" align="center" prop="working">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.working"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['aero:staff:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['aero:staff:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改员工信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工姓名" prop="staffName">
          <el-input v-model="form.staffName" placeholder="请输入员工姓名" />
        </el-form-item>
        <el-form-item label="员工编号" prop="staffNo">
          <el-input v-model="form.staffNo" placeholder="请输入员工编号" :disabled="form.postNo === SysPostCode().DIRECTOR" />
        </el-form-item>
        <el-form-item label="员工性别" prop="gender">
          <el-radio-group v-model="form.gender" :disabled="form.postNo === SysPostCode().DIRECTOR">
            <el-radio
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" :disabled="form.postNo === SysPostCode().DIRECTOR"/>
        </el-form-item>
        <el-form-item label="岗位" prop="postNo">
          <el-select v-model="form.postNo" placeholder="请选择岗位" clearable :disabled="form.postNo === SysPostCode().DIRECTOR">
            <el-option
              v-for="item in sysPostList"
              :key="item.postCode"
              :label="item.postName"
              :value="item.postCode"
              :disabled="item.postCode === SysPostCode().DIRECTOR"
            />
          </el-select>
        </el-form-item>
<!--        <el-form-item label="所属车间" prop="workshopId" v-if="form.postNo === SysPostCode().WORK_SHOP_DIRECTOR">-->
<!--          <el-select v-model="form.workshopId" placeholder="请选择车间" clearable>-->
<!--            <el-option-->
<!--              v-for="item in workshopList"-->
<!--              :key="item.value"-->
<!--              :label="item.label"-->
<!--              :value="item.value"-->
<!--            />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="所属班组" prop="groupId" v-if="form.postNo === SysPostCode().STAFF">
          <el-cascader
            v-model="form.groupId"
            :options="this.workshopList"
            :props="{ expandTrigger: 'hover', emitPath:false }" clearable></el-cascader>
        </el-form-item>
        <el-form-item label="是否在职" prop="working">
          <el-radio-group v-model="form.working" :disabled="form.postNo === SysPostCode().DIRECTOR">
            <el-radio
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStaff, getStaff, delStaff, addStaff, updateStaff } from "@/api/aero/staff";
import { listAllWorkshop } from "@/api/aero/workshop";
import { listAllPost } from "@/api/system/post";
import { SysPostCode } from "@/enums";

export default {
  name: "Staff",
  dicts: ['sys_yes_no', 'sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工信息表格数据
      staffList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        staffName: null,
        staffNo: null,
        gender: null,
        workshopNo: null,
        postNo: null,
        working: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        staffName: [
          { required: true, message: "员工姓名不能为空", trigger: "blur" }
        ],
        staffNo: [
          { required: true, message: "员工编号不能为空", trigger: "blur" }
        ],
        gender: [
          { required: true, message: "员工性别不能为空", trigger: "change" }
        ],
        workshopNo: [
          { required: true, message: "所属车间编码不能为空", trigger: "blur" }
        ],
        postNo: [
          { required: true, message: "岗位编码不能为空", trigger: "blur" }
        ],
        working: [
          { required: true, message: "是否在职不能为空", trigger: "change" }
        ],
        phone: [
          { required: true, message: "电话不能为空", trigger: "change" }
        ],
      },
      // 车间信息列表
      workshopList: [],
      // 岗位信息列表
      sysPostList: []
    };
  },
  created() {
    this.getList();
    this.getWorkshopList();
    this.getSysPostList();
  },
  activated() {
    this.getWorkshopList();
    this.getSysPostList();
  },
  methods: {
    SysPostCode() {
      return SysPostCode;
    },
    /** 查询员工信息列表 */
    getList() {
      this.loading = true;
      listStaff(this.queryParams).then(response => {
        this.staffList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        staffId: null,
        staffName: null,
        staffNo: null,
        gender: null,
        phone: null,
        workshopNo: null,
        postNo: null,
        working: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.staffId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加员工信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const staffId = row.staffId || this.ids
      getStaff(staffId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改员工信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.staffId != null) {
            updateStaff(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStaff(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const staffIds = row.staffId || this.ids;
      this.$modal.confirm('是否确认删除员工信息编号为"' + staffIds + '"的数据项？').then(function() {
        return delStaff(staffIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('aero/staff/export', {
        ...this.queryParams
      }, `staff_${new Date().getTime()}.xlsx`)
    },
    /** 获取车间信息列表 */
    getWorkshopList() {
      listAllWorkshop(null).then(response => {
        let workshopList = response.data;
        if (!workshopList || workshopList.length <= 0) return;

        let temp = [];
        for (let i in workshopList) {
          let workshop = {};
          workshop.label = workshopList[i].workshopNo;
          workshop.value = workshopList[i].workshopId;

          let children = [];
          if (!workshopList[i].groupInfoList || workshopList[i].groupInfoList.length <= 0) continue;
          for (let j in workshopList[i].groupInfoList) {
            let groupInfo = {};
            groupInfo.label = workshopList[i].groupInfoList[j].groupNo;
            groupInfo.value = workshopList[i].groupInfoList[j].groupId;
            children.push(groupInfo);
          }
          workshop.children = children;

          temp.push(workshop);
        }
        this.workshopList = temp;
      })
    },
    /** 获取岗位信息列表 */
    getSysPostList() {
      listAllPost(null).then(response => {
        this.sysPostList = response.data;
      })
    }
  }
};
</script>
